package com.winderinfo.yikaotianxia.ui.mockexam;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaselanFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.LogUtil;
import com.winderinfo.yikaotianxia.core.utils.MapUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;
import com.winderinfo.yikaotianxia.ui.report.ReportActivity;
import com.winderinfo.yikaotianxia.ui.report.ReportBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class AnswerCardFragment extends BaselanFragment implements Contract.View {


    @BindView(R.id.item_dan)
    RecyclerView itemDan;
    @BindView(R.id.item_duo)
    RecyclerView itemDuo;
    @BindView(R.id.item_but)
    Button itemBut;

    private Presenter presenter;
    private Map<String, Object> map = new HashMap<>();
    private int id;
    private int cardid;

    @Override
    protected int layoutResID() {
        return R.layout.item_card;
    }


    @Override
    protected void initView(View view) {

    }


    @Override
    protected void loadData() {
        presenter = new Presenter(this);
        cardid = getArguments().getInt("cardid");
        AnswerBean data = (AnswerBean) getArguments().getSerializable("data");
        id = getArguments().getInt("id");
        List<AnswerBean.RowsBean> rows = data.getRows();
        List<AnswerBean.RowsBean> danList = new ArrayList<>();
        List<AnswerBean.RowsBean> duoList = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++) {
            if (rows.get(i).getTitleTypeId() == 1) {
                danList.add(rows.get(i));
            }
            if (rows.get(i).getTitleTypeId() == 2) {
                duoList.add(rows.get(i));
            }
        }

        //单
        itemDan.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        OptionAdapter optionAdapter = new OptionAdapter(getActivity(), danList);
        itemDan.setAdapter(optionAdapter);
        //多
        itemDuo.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        MultipleAdapter multipleAdapter = new MultipleAdapter(getActivity(), duoList);
        itemDuo.setAdapter(multipleAdapter);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.item_but)
    public void onViewClicked() {
        List<CardBean> cardBeans = new ArrayList<>();
        for (String key : MapUtil.cardBeanMap.keySet()) {
            cardBeans.add(MapUtil.cardBeanMap.get(key));
        }
        Gson gson = new Gson();
        String s = gson.toJson(cardBeans);

        map.clear();
        map.put("ykAnswerRecords", s);
        presenter.onPostRequest(Api.ADD, map, ReportBean.class);

    }

    @Override
    public void onSuccess(Object data) {
        if (data instanceof ReportBean) {
            ReportBean.YkScoreBean ykScore = ((ReportBean) data).getYkScore();
            Bundle bundle = new Bundle();
            bundle.putSerializable("reoprt", ykScore);
            bundle.putInt("id",cardid);
            openActivityAndCloseThis(ReportActivity.class, bundle);
        }
    }

    @Override
    public void onFailure(Object error) {

    }
}
